import router from './router';
import { getStorage } from '@/utils';
import { useGlobal } from '@/store/index';

// 路由白名单
const whiteList = ['/login', '/register'];

router.beforeEach((to, from, next) => {
  if (getStorage({ key: 'test-token', islocal: true })) {
    if (to.path === '/login') {
      next({ path: '/' });
    } else {
      const store = useGlobal();
      store.setTitle(to.meta.title as string);
      next();
    }
  } else {
    if (whiteList.includes(to.path)) {
      const store = useGlobal();
      store.setTitle(to.meta.title as string);
      next();
    } else {
      next('/login');
    }
  }
});
